import { Injectable } from '@nestjs/common'
import { CreateSupportDto } from './dto/create-support.dto'
import { PrismaService } from 'src/common/prisma/prisma.service'

@Injectable()
export class SupportService {
   constructor(private readonly prisma: PrismaService) {}
   async create(createSupportDto: CreateSupportDto, userId: number) {
      return await this.prisma.support.create({
         data: {
            user: { connect: { id: userId } },
            artiel: { connect: { id: +createSupportDto.articleId } }
         }
      })
   }

   async findAll(id: number) {
      return await this.prisma.support.findMany({
         where: {
            articelId: id
         },
         include: {
            user: true
         }
      })
   }

   async countNum(articelId: number) {
      const res = await this.prisma.support.count({
         where: { articelId }
      })

      return { num: res }
   }

   async findUserOne(articleId: number, userId: number) {
      return await this.prisma.support.findFirst({
         where: { userId: +userId, AND: { articelId: +articleId } },
         include: {
            user: true
         }
      })
   }

   async remove(id: number) {
      return await this.prisma.support.delete({
         where: { id }
      })
   }
}
